package com.dc.auth.service.api.impl;

import com.alibaba.fastjson.JSONObject;
import com.dc.auth.mapper.AppInfoMapper;
import com.dc.auth.mapper.entity.DcAppInfo;
import com.dc.auth.service.IAuthorizationService;
import com.dc.base.BaseApiService;
import com.dc.base.BaseResponse;
import com.dc.core.token.GenerateToken;
import com.dc.utils.Guid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthorizationServiceImpl extends BaseApiService<JSONObject> implements IAuthorizationService {

    @Autowired
    private AppInfoMapper appInfoMapper;

    @Autowired
    private GenerateToken generateToken;

    @Override
    public BaseResponse<JSONObject> applyAppInfo(String appName) {
        //1、验证参数
        if (StringUtils.isEmpty(appName)) {
            return setResultError("机构名称不能为空");
        }
        //2、生成appid 和 appSecret
        Guid guid = new Guid();
        String appId = guid.getAppId();
        String appScrect = guid.getAppScrect();
        //3、添加数据库中
        DcAppInfo dcAppInfo = new DcAppInfo(appName, appId, appScrect);
        int insertAppInfo = appInfoMapper.insertAppInfo(dcAppInfo);
        if (!toDaoResult(insertAppInfo)) {
            return setResultError("申请失败");
        }
        //4、返回给客户端
        JSONObject data = new JSONObject();
        data.put("appId", appId);
        data.put("appScrect", appScrect);
        return setResultSuccess(data);
    }

    @Override
    public BaseResponse<JSONObject> getAccessToken(String appId, String appSecret) {
        //使用 appId + appSecret 获取AccessToken
        //1、参数验证
        if (StringUtils.isEmpty(appId)) {
            return setResultError("appId不能为空!");
        }
        if (StringUtils.isEmpty(appSecret)) {
            return setResultError("appSecret不能为空!");
        }
        // 2、使用appId+appSecret查询数据库
        DcAppInfo dcAppInfo = appInfoMapper.selectByAppInfo(appId, appSecret);
        if (dcAppInfo == null) {
            return setResultError("appId或者是appSecret错误!");
        }
        //3、获取应用机构信息  生成accessToken
        String dbAppId = dcAppInfo.getAppId();
        String accessToken = generateToken.createToken("auth", dbAppId);  // 创建之前查询一下，是否已经存在了，需要删除，通过表去维护
        JSONObject data = new JSONObject();
        data.put("accessToken", accessToken);
        return setResultSuccess(data);
    }

    @Override
    public BaseResponse<JSONObject> getAppInfo(String accessToken) {
        if (StringUtils.isEmpty(accessToken)) {
            return setResultError("AccessToken cannot be empty ");
        }
        String appId = generateToken.getToken(accessToken);
        if (StringUtils.isEmpty(appId)) {
            return setResultError("accessToken  invalid");
        }
        DcAppInfo dcAppInfo = appInfoMapper.findByAppInfo(appId);
        if (dcAppInfo == null) {
            return setResultError("AccessToken  invalid");
        }
        JSONObject data = new JSONObject();
        data.put("appInfo", dcAppInfo);
        return setResultSuccess(data);
    }
}
